package com.nhjf.mobile.sso;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;




public class SSOUtil {
	
	/** key */
	private static String KEY = "crypassword";
	
	/**
	 * 加密
	 * 
	 * @param aid
	 * @param member
	 * @param username
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 * @throws NoSuchPaddingException
	 * @throws InvalidKeyException
	 * @throws InvalidAlgorithmParameterException
	 * @throws IllegalStateException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws UnsupportedEncodingException
	 */
	public static String encryptUserNamePassword(String aid, String member,String password,
			String username,String orderNum,String orderStatus) throws NoSuchAlgorithmException,
			InvalidKeySpecException, NoSuchPaddingException,
			InvalidKeyException, InvalidAlgorithmParameterException,
			IllegalStateException, IllegalBlockSizeException,
			BadPaddingException, UnsupportedEncodingException {
		DESKeySpec pbeKeySpec;
		// DESParameters pbeParamSpec;
		SecretKeyFactory keyFac;

		SecureRandom sr = new SecureRandom();
		// pbeParamSpec = new DESParameters(salt);

		pbeKeySpec = new DESKeySpec(KEY.getBytes());
		keyFac = SecretKeyFactory.getInstance("DES");
		SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

		// Create PBE Cipher
		Cipher pbeCipher = Cipher.getInstance("DES");

		// Initialize PBE Cipher with key and parameters
		pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, sr);
		
		System.out.println(member + "##" + password + "##" + username);
		// Encrypt the cleartext
		byte[] ciphertext = pbeCipher
				.doFinal((member + "##" + password + "##" + username)
						.getBytes("UTF8"));
		String string = new String(Base64Coder.encode(ciphertext));
//		System.out.println("encode:" + string);
//		string = string.replaceAll("[\n=]", "");
//		System.out.println("encode2:" + string);
		return string;

	}

	/**
	 * 加密2
	 * 
	 * @param aid
	 * @param member
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 * @throws NoSuchPaddingException
	 * @throws InvalidKeyException
	 * @throws InvalidAlgorithmParameterException
	 * @throws IllegalStateException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws UnsupportedEncodingException
	 */
	public static String encryptUserNamePassword2(String member,String password) throws NoSuchAlgorithmException,
			InvalidKeySpecException, NoSuchPaddingException,
			InvalidKeyException, InvalidAlgorithmParameterException,
			IllegalStateException, IllegalBlockSizeException,
			BadPaddingException, UnsupportedEncodingException {
		DESKeySpec pbeKeySpec;
		// DESParameters pbeParamSpec;
		SecretKeyFactory keyFac;

		SecureRandom sr = new SecureRandom();
		// pbeParamSpec = new DESParameters(salt);

		pbeKeySpec = new DESKeySpec(KEY.getBytes());
		keyFac = SecretKeyFactory.getInstance("DES");
		SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

		// Create PBE Cipher
		Cipher pbeCipher = Cipher.getInstance("DES");

		// Initialize PBE Cipher with key and parameters
		pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, sr);
		
		System.out.println(member + "##" + password);
		// Encrypt the cleartext
		byte[] ciphertext = pbeCipher
				.doFinal((member + "##" + password)
						.getBytes("UTF8"));
		String string = new String(Base64Coder.encode(ciphertext));
//		System.out.println("encode:" + string);
//		string = string.replaceAll("[\n=]", "");
//		System.out.println("encode2:" + string);
		return string;

	}
	
	/**
	 * 解密
	 * 
	 * @param userNamepassword
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 * @throws NoSuchPaddingException
	 * @throws InvalidKeyException
	 * @throws InvalidAlgorithmParameterException
	 * @throws IllegalStateException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws UnsupportedEncodingException
	 */
	public static String decryptUserNamePassword(String userNamepassword)
			throws SecurityException, NoSuchAlgorithmException,
			InvalidKeySpecException, NoSuchPaddingException,
			InvalidKeyException, InvalidAlgorithmParameterException,
			IllegalStateException, IllegalBlockSizeException,
			BadPaddingException, UnsupportedEncodingException {
		System.out.println("cookie中读取的加密串值为:" + userNamepassword);
		//log.info("+++++++++++++ before isBlank: userNamepassword:" + userNamepassword);
		
		DESKeySpec pbeKeySpec;
		SecretKeyFactory keyFac;

		if(StringUtils.isBlank(userNamepassword)){
			return null;
		}
		
		if(userNamepassword.startsWith("\"")){
			userNamepassword = userNamepassword.substring(1);
		}
		if(userNamepassword.endsWith("\"")){
			userNamepassword = userNamepassword.substring(0,userNamepassword.length()-1);
		}
	
		SecureRandom sr = new SecureRandom();

		pbeKeySpec = new DESKeySpec(KEY.getBytes());
		keyFac = SecretKeyFactory.getInstance("DES");
		SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

		Cipher pbeCipher = Cipher.getInstance("DES");

		pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, sr);

		byte[] bs = Base64Coder.decode(userNamepassword);
			
		byte[] ciphertext = pbeCipher.doFinal(bs);
		String string = new String(ciphertext, "UTF8");
		// System.out.println(string);
		return string;
	}
	
	
	
	
	/**
	 * 返回解密后的用户名
	 * 
	 * @param request
	 * @return
	 */
//	public String getUserName(HttpServletRequest request) {
//		Cookie cookie = ReserverCookie.getCookie(request, null);
//		if (cookie != null && cookie.getValue() != null) {
//			try {
//				String string = decryptUserNamePassword(cookie.getValue());
//				if (string != null && string.indexOf("##") > 0) {
//					int ix = string.indexOf("##");
//					return string.substring(0, ix);
//				}
//				return string;
//			} catch (Exception e) {
//				e.printStackTrace();
//				throw new RuntimeException(e);
//			}
//		}
//		return null;
//	}

	
	public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalStateException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {

//		System.out.println(
//				encryptUserNamePassword("", "018607750512", "00801000", "FFP"," ", " "));
//		System.out.println(
//				decryptUserNamePassword(encryptUserNamePassword("", "ahyyxx222", "00801000", "C"," ", " ")));
//		System.out.println(
//				decryptUserNamePassword("y2b 35XltryRe n0qez7BJgVae2sOduJaSF58h533OM="));
		System.out.println(
				decryptUserNamePassword("1558dbf9e1d63a9d004a55485880d4ec"));
	}
}
